package com.jxwifi.cloud.quickclean.upgrade;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log;

import org.simple.eventbus.EventBus;

/**
 * Created by ${oushuhua} on 2018/6/4.
 */

public class NetWorkStateReceiver extends BroadcastReceiver {

    private boolean mindexStr = true;
    private int mIsDi = 200;

    @Override
    public void onReceive(Context context, Intent intent) {

        System.out.println("网络状态发生变化");
        //检测API是不是小于23，因为到了API23之后getNetworkInfo(int networkType)方法被弃用
        //android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            //获得ConnectivityManager对象
            ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            //获取ConnectivityManager对象对应的NetworkInfo对象
            //获取WIFI连接的信息
            NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            //获取移动数据连接的信息
            NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
                //Toast.makeText(context, "WIFI已连接,移动数据已连接", Toast.LENGTH_SHORT).show();
                mIsDi = 100;
                //EventBus.getDefault().post(true, "e-NetWorkState");
            } else if (wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) {
                if (mIsDi == 200) {
                    mIsDi = 100;
                    EventBus.getDefault().post(true, "e-NetWorkState");
                } else {
                    mIsDi = 200;
                }
                //Log.w("test", "WIFI已连接,移动数据已断开" + mIsDi);
            } else if (!wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
                mIsDi = 100;
                //Log.w("test", "WIFI已断开,移动数据已连接" + mIsDi);
                EventBus.getDefault().post(true, "e-NetWorkState");
            } else {
                mIsDi = 100;
                EventBus.getDefault().post(false, "e-NetWorkState");
                //Log.w("test", "WIFI已断开,移动数据已断开" + mIsDi);
            }
            //API大于23时使用下面的方式进行网络监听
        } else {
            System.out.println("API level 大于23");
            //获得ConnectivityManager对象
            ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            //获取所有网络连接的信息
            Network[] networks = connMgr.getAllNetworks();
            //用于存放网络连接信息
            StringBuilder sb = new StringBuilder();
            //通过循环将网络信息逐个取出来
            for (int i = 0; i < networks.length; i++) {
                //获取ConnectivityManager对象对应的NetworkInfo对象
                NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
                sb.append(networkInfo.getTypeName() + " is " + networkInfo.isConnected());
            }
            //MOBILE  WIFI
            if (networks.length > 0) {
                Log.w("test", "网络状态  " + sb.toString());
                if (!"MOBILE is trueWIFI is true".equals(sb.toString())) {
                    if ("MOBILE is falseWIFI is true".equals(sb.toString())) { //在网络切换的情况下，有可能出现多次执行这里， 而这里的EventBus不能有多次的情况出现，所以做了这个判断
                        if (mindexStr) {
                            mindexStr = false;
                            EventBus.getDefault().post(true, "e-NetWorkState");
                            //Log.w("test", "网络判断  执行EventBus");
                        }
                    } else {
                        mindexStr = true;
                        EventBus.getDefault().post(true, "e-NetWorkState");
                        //Log.w("test", "网络判断  执行EventBus");
                    }
                }
            } else {
                EventBus.getDefault().post(false, "e-NetWorkState");
            }
        }
    }
}
